#!/usr/bin/env python3
"""
检查数据库中的数据结构
"""

from app import create_app
from app.services.civil_code_service import CivilCodeService

def check_data():
    """检查数据库数据"""
    app = create_app()
    
    with app.app_context():
        try:
            service = CivilCodeService()
            
            # 检查统计信息
            stats = service.get_statistics()
            print("=== 数据库统计信息 ===")
            for key, value in stats.items():
                print(f"{key}: {value}")
            
            # 尝试通用搜索
            print("\n=== 尝试搜索测试 ===")
            search_result = service.smart_search("合同", per_page=5)
            print(f"搜索'合同'结果: {search_result.get('total', 0)} 条")
            
            if search_result.get('articles'):
                print("示例法条:")
                for i, article in enumerate(search_result['articles'][:3], 1):
                    print(f"  {i}. {article.get('article_number', 'N/A')}: {article.get('title', 'N/A')}")
                    
            # 检查章节列表
            print("\n=== 章节列表 ===")
            chapters = service.get_chapters_list()
            print(f"章节总数: {len(chapters)}")
            for chapter in chapters[:10]:  # 显示前10个
                print(f"  - {chapter}")
                
        except Exception as e:
            print(f"检查失败: {str(e)}")
            import traceback
            traceback.print_exc()

if __name__ == '__main__':
    check_data()

